package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.WorkflowBusinessRpcService;
import com.hunttown.mes.rpc.domain.WorkflowBusinessDTO;
import com.hunttown.mes.rpc.domain.query.WorkflowBusinessDTOQuery;
import com.hunttown.mes.service.WorkflowBusinessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.WorkflowBusinessConverts.convert;

/**
 * created by wangjunfu on 2019-08-01 16:55:22
 */
@Service
public class WorkflowBusinessRpcServiceImpl implements WorkflowBusinessRpcService {

    private final Logger logger = LoggerFactory.getLogger(WorkflowBusinessRpcServiceImpl.class);

    private final WorkflowBusinessService workflowBusinessService;

    @Autowired
    public WorkflowBusinessRpcServiceImpl(WorkflowBusinessService workflowBusinessService) {
        this.workflowBusinessService = workflowBusinessService;
    }

    @Override
    public WorkflowBusinessDTO insert(WorkflowBusinessDTO workflowBusinessDTO) {
        try {
            return convert(workflowBusinessService.insert(convert(workflowBusinessDTO)));
        } catch (Exception e) {
            logger.error("WorkflowBusiness create failed.", e);
            throw new RuntimeException("WorkflowBusiness create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(WorkflowBusinessDTO workflowBusinessDTO) {
        try {
            return workflowBusinessService.updateInfoById(convert(workflowBusinessDTO));
        } catch (Exception e) {
            logger.error("WorkflowBusiness update by id failed.", e);
            throw new RuntimeException("WorkflowBusiness update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(WorkflowBusinessDTOQuery workflowBusinessDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (workflowBusinessDTOQuery.getW_id() != null && workflowBusinessDTOQuery.getW_id() > 0) {
                query.put("w_id", workflowBusinessDTOQuery.getW_id());
            }

            if (workflowBusinessDTOQuery.getW_deleteFlag() != null) {
                query.put("w_deleteFlag", workflowBusinessDTOQuery.getW_deleteFlag());
            }

            if (workflowBusinessDTOQuery.getIds() != null && !workflowBusinessDTOQuery.getIds().equals("")) {
                query.put("ids", workflowBusinessDTOQuery.getIds());
            }

            if (workflowBusinessDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", workflowBusinessDTOQuery.getStopFlag());
            }

            if (workflowBusinessDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", workflowBusinessDTOQuery.getDeleteFlag());
            }

            return workflowBusinessService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("WorkflowBusiness update by query failed.", e);
            throw new RuntimeException("WorkflowBusiness update by query failed, Please order your code.", e);
        }
    }

    @Override
    public WorkflowBusinessDTO selectById(Integer id) {
        try {
            return convert(workflowBusinessService.selectById(id));
        } catch (Exception e) {
            logger.error("WorkflowBusiness select by id failed.", e);
            throw new RuntimeException("WorkflowBusiness select by id failed, Please order your code.", e);
        }
    }

    @Override
    public WorkflowBusinessDTO selectObjByQuery(WorkflowBusinessDTOQuery workflowBusinessDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (StringUtils.isNotBlank(workflowBusinessDTOQuery.getBusinessNameEn())) {
                query.put("businessNameEn", workflowBusinessDTOQuery.getBusinessNameEn());
            }

            if (workflowBusinessDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", workflowBusinessDTOQuery.getStopFlag());
            }

            if (workflowBusinessDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", workflowBusinessDTOQuery.getDeleteFlag());
            }

            return convert(workflowBusinessService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("WorkflowBusiness select obj by query failed.", e);
            throw new RuntimeException("WorkflowBusiness select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<WorkflowBusinessDTO> getListForPage(WorkflowBusinessDTOQuery workflowBusinessDTOQuery) {
        Query query = Query.page(workflowBusinessDTOQuery.getPn(), workflowBusinessDTOQuery.getPageSize());

        if (workflowBusinessDTOQuery.getId() != null && workflowBusinessDTOQuery.getId() > 0) {
            query.put("id", workflowBusinessDTOQuery.getId());
        }

        if (workflowBusinessDTOQuery.getIds() != null && !workflowBusinessDTOQuery.getIds().equals("")) {
            query.put("ids", workflowBusinessDTOQuery.getIds());
        }

        if (workflowBusinessDTOQuery.getEntId() != null) {
            query.put("entId", workflowBusinessDTOQuery.getEntId());
        }

        if (StringUtils.isNotBlank(workflowBusinessDTOQuery.getLikeName())) {
            query.put("likeName", workflowBusinessDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(workflowBusinessDTOQuery.getBusinessNameEn())) {
            query.put("businessNameEn", workflowBusinessDTOQuery.getBusinessNameEn());
        }

        if (workflowBusinessDTOQuery.getCatalogId() != null) {
            query.put("catalogId", workflowBusinessDTOQuery.getCatalogId());
        }

        if (workflowBusinessDTOQuery.getBusinessType() != null) {
            query.put("businessType", workflowBusinessDTOQuery.getBusinessType());
        }

        if (StringUtils.isNotBlank(workflowBusinessDTOQuery.getOrderBy())) {
            query.put("orderBy", workflowBusinessDTOQuery.getOrderBy());
        }

        if (workflowBusinessDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", workflowBusinessDTOQuery.getStopFlag());
        }

        if (workflowBusinessDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", workflowBusinessDTOQuery.getDeleteFlag());
        }

        List<WorkflowBusinessDTO> list = convert(workflowBusinessService.selectByQuery(query));
        Page<WorkflowBusinessDTO> page = Page.from(workflowBusinessDTOQuery.getPn(), workflowBusinessDTOQuery.getPageSize(), list);
        page.setTotalItems(workflowBusinessService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return workflowBusinessService.deleteById(id);
        } catch (Exception e) {
            logger.error("WorkflowBusiness delete failed.", e);
            throw new RuntimeException("WorkflowBusiness delete failed, Please order your code.", e);
        }
    }
}